<template>
  <div>
    <!--引用表格-->
   <BasicTable @register="registerTable" :rowSelection="rowSelection" @row-click="selectChange">
     <!--插槽:table标题-->
      <template #tableTitle>
          <!--
          <a-button type="primary" @click="handleAdd" preIcon="ant-design:plus-outlined"> 新增</a-button>-->
          <a-button type="primary" @click="bindCard" preIcon="ant-design:plus-outlined">学生绑定</a-button>
          <!--
          <j-upload-button  type="primary" preIcon="ant-design:import-outlined" @click="onImportXls">导入</j-upload-button>
          <a-dropdown v-if="selectedRowKeys.length > 0">
              <template #overlay>
                <a-menu>
                  <a-menu-item key="1" @click="sendCmd(1)">
                    <Icon icon="ant-design:delete-outlined"></Icon>
                    待机模式
                  </a-menu-item>
                  <a-menu-item key="1" @click="sendCmd(2)">
                    <Icon icon="ant-design:delete-outlined"></Icon>
                    定位模式
                  </a-menu-item>
                  <a-menu-item key="1" @click="sendCmd(3)">
                    <Icon icon="ant-design:delete-outlined"></Icon>
                    亲情号码设置
                  </a-menu-item>
                  <a-menu-item key="1" @click="sendCmd(4)">
                    <Icon icon="ant-design:delete-outlined"></Icon>
                    位置上送频率
                  </a-menu-item>
                  <a-menu-item key="1" @click="sendCmd(5)">
                    <Icon icon="ant-design:delete-outlined"></Icon>
                    设置闹钟
                  </a-menu-item>
                  <a-menu-item key="1" @click="sendCmd(5)">
                    <Icon icon="ant-design:delete-outlined"></Icon>
                    上课免打扰设置
                  </a-menu-item>
                </a-menu>
              </template>
              <a-button>指令下发
                <Icon icon="mdi:chevron-down"></Icon>
              </a-button>
        </a-dropdown>-->
      </template>

      <template #useStatusSlot="{ record }">
        <Tag v-if="record.useStatus=='1'" color="green">正常</Tag>
        <Tag v-if="record.useStatus=='2'" color="orange">禁用</Tag>
        <Tag v-if="record.useStatus=='3'" color="red">已损坏</Tag>
      </template>

      <template #activationStatusSlot="{ record }">
        <Tag v-if="record.activationStatus=='1'" color="green">已激活</Tag>
        <Tag v-if="record.activationStatus=='2'" color="red">未激活</Tag>
      </template>

      <template #bindingStatusSlot="{ record }">
        <Tag v-if="record.bindingStatus=='1'" color="green">已绑定</Tag>
        <Tag v-if="record.bindingStatus=='2'" color="red">未绑定</Tag>
      </template>

      <template #parBindingStatusSlot="{ record }">
        <Tag v-if="record.parBindingStatus=='1'" color="green">已绑定</Tag>
        <Tag v-if="record.parBindingStatus=='2'" color="red">未绑定</Tag>
      </template>

      <template #expireStatusSlot="{ record }">
        <Tag v-if="record.expireStatus=='0'" color="green">未过期</Tag>
        <Tag v-if="record.expireStatus=='1'" color="red">已过期</Tag>
      </template>

      <template #disturbStatusSlot="{ record }">
        <Tag v-if="record.disturbStatus=='1'" color="green">开启</Tag>
        <Tag v-if="record.disturbStatus=='2'" color="red">关闭</Tag>
      </template>

      <template #onlineStatusSlot="{ record }">
        <Tag v-if="record.onlineStatus=='1'" color="green">在线</Tag>
        <Tag v-if="record.onlineStatus=='2'" color="red">离线</Tag>
        <Tag v-if="record.onlineStatus=='4'" color="red">已过期</Tag>
      </template>

       <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)"/>
      </template>
    </BasicTable>
    <!-- 表单区域 -->
    <BindCardModal @register="registerForm" @success="handleSuccess"></BindCardModal>
    <CmdModel @register="registerCmdForm" @success="handleSuccess"></CmdModel>
    <BindParentsDrawer @register="registerParentDrawer"  @success="handleSuccess"/>
  </div>
</template>

<script lang="ts" name="esidcard-card" setup>
  import {ref, reactive, toRaw} from 'vue';
  import {BasicTable, TableAction} from '/@/components/Table';
  import {useModal} from '/@/components/Modal';
  import { Tag } from 'ant-design-vue';
  import { useListPage } from '/@/hooks/system/useListPage'
  import BindCardModal from './components/BindCardModal.vue'
  import BindParentsDrawer from './components/BindParentsDrawer.vue'
  import CmdModel from './components/CmdModel.vue'
  import {columns, searchFormSchema} from './Card.data';
  import {list, deleteOne, getImportUrl,getExportUrl,unbind,unbindParents} from './Card.api';
  import { useDrawer } from '/@/components/Drawer';
  const queryParam = reactive<any>({});
  //注册model
  const [registerForm, {openModal:openForm}] = useModal();
  const [registerCmdForm, {openModal:openCmdForm}] = useModal();
  const [registerParentDrawer, {  openDrawer: showParentsDrawer }] = useDrawer();
  
  
  //注册table数据
  const { tableContext } = useListPage({
      tableProps:{
           title: '电子学生证',
           api: list,
           columns,
           canResize:false,
            clickToRowSelect:true,
           showActionColumn:true,
            rowSelection:{type:'radio'},
           formConfig: {
              //labelWidth: 120,
              schemas: searchFormSchema,
              autoSubmitOnEnter:true,
              fieldMapToNumber: [
              ],
              fieldMapToTime: [
              ],
            },
           actionColumn: {
               width: 120,
               fixed:'right'
            },
            beforeFetch: (params) => {
              return Object.assign(params, queryParam);
            },
      },
       exportConfig: {
            name:"电子学生证",
            url: getExportUrl,
            params: queryParam,
          },
          importConfig: {
            url: getImportUrl,
            success: handleSuccess
          },
  })

  const [registerTable, {reload},{ rowSelection, selectedRowKeys }] = tableContext

  const imei = ref('');

 function selectChange(record: Recordable) {
    imei.value = record.imei
    console.log(record.imei)
 }
  
  
  function bindCard() {
     openForm(true, {
       record:{imei:imei.value},
       isUpdate: false,
       showFooter: true,
       
     });
  }
  function sendCmd(optype) {
    let record = toRaw(rowSelection.selectedRows)[0];
     openCmdForm(true, {
       isUpdate: true,
       optype: optype,
       record:record,
       showFooter: true,
     });
  }
   
   /**
    * 删除事件
    */
  async function handleDelete(record) {
     await deleteOne({cardId: record.cardId}, handleSuccess);
   }
   /**
    * 学生解绑
    */
  async function unBindCard(record) {
     await unbind({imei: record.imei}, handleSuccess);
   }
   /**
    * 学生解绑
    */
  async function unBindCardParents(record) {
     await unbindParents({imei: record.imei,cardId:record.cardId}, handleSuccess);
   }
   async function showParents(record) {
     showParentsDrawer(true, record);
   }
   
   /**
    * 成功回调
    */
  function handleSuccess() {
      (selectedRowKeys.value = []) && reload();
   }
   function getTableAction(record){
        return [{
          label: '查看绑定',
          onClick: showParents.bind(null, record),
        }];
   }
     /**
        * 下拉操作栏
        */
  function getDropDownAction(record){
       let menu= [{}];
       if(record.bindingStatus=='1') {
          menu.push({
           label: '学生解绑',
           onClick: unBindCard.bind(null, record),
         });
       }

       if(record.parBindingStatus=='1') {
         menu.push({
           label: '家长解绑',
           onClick: unBindCardParents.bind(null, record),
         });
       }

      menu.push({
           label: '删除',
           popConfirm: {
             title: '是否确认删除',
             confirm: handleDelete.bind(null, record),
             placement: 'topLeft',
           }
      });

       return menu;
   }


</script>

<style scoped>

</style>